package com.mybills.proj;
import java.net.URL;
import java.util.Calendar;
import java.util.Vector;
import java.lang.*;

import android.os.AsyncTask;

import com.google.gdata.client.Query;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gdata.data.calendar.ColorProperty;
import com.google.gdata.data.calendar.HiddenProperty;
import com.google.gdata.data.calendar.TimeZoneProperty;
import com.google.gdata.data.extensions.Reminder;
import com.google.gdata.data.extensions.When;
import com.google.gdata.data.extensions.Where;
import com.google.gdata.data.extensions.Reminder.Method;
import com.google.gdata.util.AuthenticationException;


public class pf {
	public class googlecal extends AsyncTask<String, Void, String>{
			
	public void create_Calendar() throws Exception
	{
		MyBllsActivity mba = new MyBllsActivity();
		CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
		CalendarEntry calendar = new CalendarEntry();
		myService.setUserCredentials(mba.getEmail(),mba.getPwd());

		// Send the request and print the response
		URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/owncalendars/full");
		CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
		calendar.setTitle(new PlainTextConstruct("new cal"));
		calendar.setSummary(new PlainTextConstruct("This calendar contains the practice schedule and game times."));
		calendar.setTimeZone(new TimeZoneProperty("America/Los_Angeles"));
		calendar.setHidden(HiddenProperty.FALSE);
		calendar.setColor(new ColorProperty("#2952A3"));
		calendar.addLocation(new Where("","","India"));
		CalendarEntry returnedCalendar = myService.insert(feedUrl, calendar);

	}
	@SuppressWarnings("static-access")
	public void createEvent(MyBllsActivity mba,details_list dl) throws Exception{
		//details_list dl = new details_list();
		String url = mba.getEmail();
		String url1 = mba.getPwd();
		String title = dl.getBill_id();
		String desc = dl.getBill_place();
		URL postUrl =
			new URL("https://www.google.com/calendar/feeds/"+url+"/private/full");
		CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
		myService.setUserCredentials(url,url1);
		
			CalendarEventEntry myEntry = new CalendarEventEntry();
			
			myEntry.setTitle(new PlainTextConstruct(title));
			myEntry.setContent(new PlainTextConstruct(desc));
            System.out.println(dl.getDate()+"------"+dl.getStart_time());
			DateTime startTime = DateTime.parseDateTime(dl.getDate()+"T"+dl.getStart_time()+":00+05:30");
			DateTime endTime = DateTime.parseDateTime(dl.getDate()+"T"+dl.getEnd_time()+":00+05:30");
			When eventTimes = new When();
			eventTimes.setStartTime(startTime);
			eventTimes.setEndTime(endTime);
			myEntry.addTime(eventTimes);
			int reminderMinutes = 15;
			Method methodType = Method.EMAIL;
           
			Reminder reminder = new Reminder();
			reminder.setMinutes(reminderMinutes);
			reminder.setMethod(methodType);
           
			myEntry.getReminder().add(reminder);
			
			
		    myEntry.setSendEventNotifications(true);
			// Send the request and receive the response:
			CalendarEventEntry insertedEntry = myService.insert(postUrl, myEntry);
	}
	
	public String[] view_calendars(MyBllsActivity mba) throws Exception{
		//MyBllsActivity mba = new MyBllsActivity();
		int count=0;
		String[] eve ;
		String url = mba.getEmail();
		String url1 = mba.getPwd();
		Vector<String> v = new Vector<String>();
		CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
		CalendarEntry calendar = new CalendarEntry();
		myService.setUserCredentials(url,url1);
		URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/owncalendars/full");
		CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
		System.out.println("Calendars you own:");
		System.out.println();
		for (int i = 0; i < resultFeed.getEntries().size(); i++) {
			  count++;
			  }
		eve = new String[count];
		for (int i = 0; i < resultFeed.getEntries().size(); i++) {
		  CalendarEntry entry = resultFeed.getEntries().get(i);
		  eve[i] = entry.getTitle().getPlainText().toString();
		  //System.out.println("\t" + v.elementAt(i));
		  }
		return eve;
	}
	public void delete_calendar()throws Exception{
		MyBllsActivity mba = new MyBllsActivity();
		URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/owncalendars/full");
		CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
		myService.setUserCredentials(mba.getEmail(),mba.getPwd());
		CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
		for (int i = 0; i < resultFeed.getEntries().size(); i++) {
		  CalendarEntry entry = resultFeed.getEntries().get(i);
		  System.out.println("Deleting calendar: " + entry.getTitle().getPlainText());
		  try {
		    entry.delete();
		  } catch (Exception e) {
		    System.out.println("\tUnable to delete primary calendar");
		  }
		}
	}
	public String[] fetchEvents(MyBllsActivity mba) throws Exception{
		//MyBllsActivity mba = new MyBllsActivity();
		// Set up the URL and the object that will handle the connection:
		String[] eve = null;
		String url = mba.getEmail();
		URL feedUrl = new URL("https://www.google.com/calendar/feeds/"+url+"/private/full");
		CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
		myService.setUserCredentials(mba.getEmail(),mba.getPwd());

		// Send the request and receive the response:
		CalendarEventFeed myFeed = myService.getFeed(feedUrl, CalendarEventFeed.class);
		
		if(myFeed.getEntries().size()>0)
		{
			eve = new String[myFeed.getEntries().size()];
			for(int i = myFeed.getEntries().size()-1;i >= 0 ; i--)
			{
				CalendarEventEntry firstMatchEntry = (CalendarEventEntry) myFeed.getEntries().get(i);
				  String myEntryTitle = firstMatchEntry.getTitle().getPlainText();
				  eve[i]=myEntryTitle;
				  //System.out.println(eve[i]);
			}
		}
		return eve; 
	}
	public void searchEvent() throws Exception{
		MyBllsActivity mba = new MyBllsActivity();
		// Set up the URL and the object that will handle the connection:
		URL feedUrl = new URL("https://www.google.com/calendar/feeds/"+mba.getEmail()+"/private/full");
		CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
		myService.setUserCredentials(mba.getEmail(),mba.getPwd());
		Query myQuery = new Query(feedUrl);
		myQuery.setFullTextQuery("appintment");
		CalendarEventFeed myResultsFeed = myService.query(myQuery,
		    CalendarEventFeed.class);
		if (myResultsFeed.getEntries().size() > 0) 
		{
		  CalendarEventEntry firstMatchEntry = (CalendarEventEntry) myResultsFeed.getEntries().get(0);
		  String myEntryTitle = firstMatchEntry.getTitle().getPlainText();
		  System.out.println(myEntryTitle);
		  }

		// Send the request and receive the response:
		//CalendarEventFeed myFeed = myService.getFeed(feedUrl, CalendarEventFeed.class);
	}
	
	@Override
	protected String doInBackground(String... params) {
		// TODO Auto-generated method stub
		for(int i = 0 ; i < 10 ; i++)
		{
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
	}
}
